# -*- coding: utf-8 -*-
"""
2024-06-11 F 表格编辑
"""
from PySide6.QtCore import Slot, QSize
from PySide6.QtGui import QAction, QKeySequence
from PySide6.QtWidgets import QMainWindow, QApplication, QVBoxLayout, QHBoxLayout, QPushButton, QWidget, QMessageBox,QDialog
from PySide6.QtCore import Qt,QModelIndex
from PySide6.QtGui import QGuiApplication, QCloseEvent
## from PySide6.QtCore.Qt import 

##from PySide6.QtCore import WindowState

##from top.wardyan.utils import winformutils
from top.wardyan.core.action.baseaction import WMainWindow,WQdialog
from top.wardyan.core.widges.basetable import WBaseTableView
from top.wardyan.core.vo.vos import Record, TableCfg, TableColCfg
from top.wardyan.core.utils.tableutil import readTableCfg, saveTableCfg,clearTableCfg




from top.wardyan.core.widges.tablemanagercoledit import *

import os




class TableEditorMain(WQdialog):

    table : WBaseTableView = None

    ## 
    widgetname : str = None
    tablename : str = None
    
    ## changed : bool = False

    def __init__(self, widgetname : str = None, tablename : str = None):
        WQdialog.__init__(self, "TableEditorMain")
        self.name = "TableEditorMain"
        self.widgetname = widgetname
        self.tablename = tablename
        # 设置窗口标志以显示最大化和最小化按钮
        self.setWindowFlags(self.windowFlags() | Qt.WindowMinMaxButtonsHint)
        ## 显示窗体具体信息
        self.main()
        ## self.setCentralWidget(WTableEditor())
        
        self.refreshTable()
    
    @Slot()
    def slot_click_btnClose(self) :
        self.close()

    
    @Slot()
    def slot_click_reset(self) :
        clearTableCfg(self.widgetname, self.tablename)
        ## 提示
        QMessageBox.information(self, "提示", "重置成功！")
    
    @Slot()
    def slot_click_autoname(self) :
        pass


    def main(self) :
        HEIGHT = 30
        layout : QVBoxLayout = QVBoxLayout()
        layout.setAlignment(Qt.AlignmentFlag.AlignTop)
        self.setLayout(layout)

        
        ctrlh : QWidget = QWidget()
        ctrlhlayout : QHBoxLayout = QHBoxLayout()
        ctrlh.setLayout(ctrlhlayout)
        ctrlhlayout.setAlignment(Qt.AlignmentFlag.AlignRight)
        layout.addWidget(ctrlh)

        ## 按钮: 搜索 脚本清单 新增脚本 表/视图结构
        btnReset : QPushButton = QPushButton("重置")
        btnReset.setMinimumHeight(50)
        btnReset.setFixedWidth(100)
        btnReset.clicked.connect(self.slot_click_reset)
        ctrlhlayout.addWidget(btnReset)
        
        btnAutoRename : QPushButton = QPushButton("自动命名")
        btnAutoRename.setMinimumHeight(50)
        btnAutoRename.setFixedWidth(100)
        btnAutoRename.clicked.connect(self.slot_click_autoname)
        ctrlhlayout.addWidget(btnAutoRename)


        btnClose : QPushButton = QPushButton("保存并关闭")
        btnClose.setMinimumHeight(50)
        btnClose.setFixedWidth(120)
        btnClose.clicked.connect(self.slot_click_btnClose)
        #btnClose.setFocus()
        btnClose.setDefault(True)
        ctrlhlayout.addWidget(btnClose)


        ctrlw : QWidget = QWidget()
        ctrllayout : QHBoxLayout = QHBoxLayout()
        ctrlw.setLayout(ctrllayout)
        layout.addWidget(ctrlw)


        ctrllayout.setAlignment(Qt.AlignmentFlag.AlignRight)
        

        ctrlt : QWidget = QWidget()
        tbllayout : QHBoxLayout = QHBoxLayout()
        ctrlt.setLayout(tbllayout)
        layout.addWidget(ctrlt)

        self.table = WBaseTableView(self, self.name, "tablemanager", dbclick = self.handle_double_click)
        tbllayout.addWidget(self.table)


    def handle_double_click(self, index: QModelIndex) -> None:
        """
        处理单元格的双击事件。
        :param index: 被双击的单元格的模型索引。
        """
        row = index.row()
        column = index.column()
        ##print(f"Double click detected at Row: {row}, Column: {column}")
        data : Record = self.table.get_row_data(row)
        if data is not None :
            field = data.get("field")
            ## win = TableColEditor("tablemanagercoledit", self.tablename, field)
            win = TableColEditor(self.widgetname, self.tablename, field)
            # win.exec()
            rs : int = win.exec()
            if rs is not None and rs > 0 :
                self.refreshTable()




    def refreshTable(self) :

        data : list[Record] = []
        # r : Record = Record()
        # r.set("name", "n1")
        # r.set("age", "2")
        # r.set("school", "??")
        # data.append(r)
        # data.append(r)
        # data.append(r)
        # data.append(r)
        # data.append(r)
        ## self.renderTable(data)

        ## 根据tablename和tablename读取配置文件
        cfg : TableCfg = readTableCfg(self.widgetname, self.tablename)
        
        height = cfg.height
        colmoveable = cfg.colmoveable

        ##
        if cfg is not None and cfg.cols is not None :
            for col in cfg.cols :
                r : Record = Record()
                r.set("idx", col.idx)
                r.set("field", col.field)
                r.set("title", col.title)
                r.set("align", col.align)
                r.set("width", col.width)
                r.set("show", col.show)
                r.set("replace", col.replace)
                r.set("datatype", col.datatype)
                r.set("format", col.format)
                r.set("sql", col.sql)
                data.append(r)
        self.table.render(data)
        
    # def exec(self) -> int:
    #     rs = super().exec()
    #     rs = 1234
    #     return rs